<?php
/**
 *   Copyright (C) 2011 - Casey Burkhardt
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/

require_once("config.inc.php");

function isInstanceActive() {
  global $ACTIVE;
  return $ACTIVE;
}

function getTitle() {
  global $LOCATION;
  if (strlen($LOCATION) != 0) { 
    return "Tightometer for " . $LOCATION;
  } else {
    return "Tightometer";
  }
}

function getLocation() {
  global $LOCATION;
  return $LOCATION;
}

function getTightnessLevel() {
  global $DATA_FILENAME;
  // Surpress warnings and return 0 if the data file can't be opened.
  $fh = @fopen($DATA_FILENAME, 'r') or FALSE;
  if ($fh === FALSE) {
    return 0;
  }
  $tightnessLevel = fread($fh, filesize($DATA_FILENAME));
  fclose($fh);
  return $tightnessLevel;
}

function setTightnessLevel($tightnessLevel) {
  global $DATA_FILENAME;
  if (strlen($tightnessLevel) <= 3) {
	$fh = fopen($DATA_FILENAME, 'w') or die("File Write Error");
    $written = fwrite($fh, $tightnessLevel);
    fclose($fh);
	return strlen($tightnessLevel) == $written;
  } else {
	return FALSE;
  }
}

function getTightnessDescription($tightnessLevel) {
  if ($tightnessLevel < 10) {
    return "Chill";
  } else if ($tightnessLevel < 20) {
    return "Bad Joke";
  } else if ($tightnessLevel < 30) {
    return "What did he just say?";
  } else if ($tightnessLevel < 40) {
    return "Hey, whatever you say man...";
  } else if ($tightnessLevel < 50) {
    return "Uncomfortable";
  } else if ($tightnessLevel < 60) {
    return "Tiiight";
  } else if ($tightnessLevel < 70) {
    return "Tight-o-Clock";
  } else if ($tightnessLevel < 80) {
    return "Half Past Tight";
  } else if ($tightnessLevel < 90) {
    return "Tightacular";
  } else if ($tightnessLevel < 100) {
    return "Tightopolis";
  } else {
    return "Tightanic";
  }
}

function verifyPassword($submission) {
  global $PASSWORD_REQUIRED, $PASSWORD;
  if ($PASSWORD_REQUIRED === TRUE) {
    if (strcmp($PASSWORD, $submission) === 0) {
      return TRUE;
    } else {
      return FALSE;
    }
  } else {
    return TRUE;
  }
}

function getRefreshRate() {
  global $REFRESH_RATE;
  return $REFRESH_RATE;
}

?>
